package com.stone.framework.user.controller;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.stone.framework.message.PageReqMessage;
import com.stone.framework.message.PageResMessage;
import com.stone.framework.message.ReqMessage;
import com.stone.framework.message.ResMessage;
import com.stone.framework.message.user.RolePageReqBody;
import com.stone.framework.message.user.RolePageResBody;
import com.stone.framework.message.user.RoleReqBody;
import com.stone.framework.user.service.RoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "角色")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "查询", httpMethod = "POST")
    @PostMapping(value = "/getPage")
    PageResMessage<RolePageResBody> getPage(@Valid @RequestBody PageReqMessage<RolePageReqBody> reqMessage) {
        IPage<RolePageResBody> page = roleService.queryPage(reqMessage.getBody(), reqMessage.buildPage());
        return PageResMessage.success(page);
    }

    @ApiOperation(value = "查询KV", httpMethod = "POST")
    @PostMapping(value = "/getKV")
    ResMessage<Map<String, String>> getKV(@Valid @RequestBody ReqMessage<Void> reqMessage) {
        Map<String, String> map = roleService.query();
        return PageResMessage.success(map);
    }

    @ApiOperation(value = "新增", httpMethod = "POST")
    @PostMapping(value = "/add")
    ResMessage<Void> add(@Valid @RequestBody ReqMessage<RoleReqBody> reqMessage) {
        roleService.save(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "修改", httpMethod = "POST")
    @PostMapping(value = "/update")
    ResMessage<Void> update(@Valid @RequestBody ReqMessage<RoleReqBody> reqMessage) {
        roleService.modify(reqMessage.getBody());
        return ResMessage.success();
    }

    @ApiOperation(value = "刪除", httpMethod = "POST")
    @PostMapping(value = "/delete")
    ResMessage<Void> delete(@Valid @RequestBody ReqMessage<Long> reqMessage) {
        roleService.delete(reqMessage.getBody());
        return ResMessage.success();
    }

}
